<template>
  <div style="width: 30%; height: 400px; margin-top: 150px; margin-left: 650px;background-color: aqua;border-radius: 5px  " >
    <h2>密码登录</h2>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" >
      <el-form-item label="用户账号" prop=username>
        <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
<!--      注册组件-->
      <router-link to="/signinbyuser" style="margin-left: 100px; color:black" >注册账号</router-link><br>
      <router-view/>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import a from "vuex-persistedstate";
import axios from "axios";

export default {
  name: "LoginView",
  data() {
    //自定校验用户名
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
        }


    };
    //自定校验密码
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      }  else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          {validator: validatePass, trigger: 'blur'}
        ],
        password: [
          {validator: validatePass2, trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //密码登录
          axios.post("/user/UserLoginByPassword ",this.ruleForm)
              .then((res)=>{
                console.log(res)
                if(res.data != null){
                  this.$message.success("登录成功!")
                }else {
                  this.$message.error("用户名或密码错误!")
                }

              })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>